package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.util.StrTools;
import com.iwhalecloud.bss.kite.cucc.service.attr.AttrModifyHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.BasedBandwidthHandler;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.SuddenBandwidthHandler;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.SuddenCycleHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.event.attr.UpdateAttrValueEvent;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.ztesoft.zsmart.core.log.ZSmartLogger;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author wanghl386
 * @version 1.0
 * @date 2021/8/27 14:20
 */

@Service
@AttrHandler(BasedBandwidthHandler.class)
public class BasedBandwidthAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    private static final ZSmartLogger LOGGER = ZSmartLogger.getLogger(BasedBandwidthAttrLinkHandler.class);

    @Override
    public void handle(KiteAttr kiteAttr, KiteInst kiteInst, AttrData attrData) {
        try {
            if (KiteStringUtils.isEqual(attrData.getServiceOfferId(), "2826")) {
                return;
            }
            KiteAttr suddenBandwidthAttr = kiteInst.getAttr("210004191");
            filterSuddenWidth(kiteInst,suddenBandwidthAttr);
            // 突发带宽之前选的不是变之后的范围，清空
            // 突发带宽
            List<KiteAttrValue> suddenList = suddenBandwidthAttr.getAttrValues();
            long count = suddenList.stream().filter(sudden -> sudden.getAttrValue().equals(suddenBandwidthAttr.getValue())).count();
            if (count == 0) {
                suddenBandwidthAttr.setValue(null);
                suddenBandwidthAttr.setValueDesc(null);
            }
        } catch (NumberFormatException e) {
            LOGGER.error("LocalCheckStartTimeAndEndTime-->", e, e.getMessage());
        }
    }

    @Override
    public void handleStyle(KiteAttr kiteAttr, KiteInst kiteInst, AttrData attrData) {

    }

    public static void filterSuddenWidth(KiteInst kiteInst, KiteAttr resAttr) {
        // 基础带宽
        KiteAttr basedBandwidthAttr = kiteInst.getAttr("200001818");
        if (!KiteStringUtils.isEmpty(basedBandwidthAttr.getValue())) {
            List<AttrValue> attrValueList = AttrValueCache.get(resAttr.getAttrId());
            AttrValue wuAttrValue= attrValueList.stream().filter(targetAttr -> KiteStringUtils.equals(targetAttr.getAttrValue(), "1_0000260")).findFirst().orElse(null);
            // 排序
            List<KiteAttrValue> attrValues =
                KiteBeanUtils.copyPropsForList(attrValueList, KiteAttrValue.class).stream().sorted((a, b) -> {
                    String valueA = a.getAttrValueDesc();
                    String valueB = b.getAttrValueDesc();
                    if (valueA.endsWith("无")) {
                        return -1;
                    } else if (valueB.endsWith("无")) {
                        return 1;
                    } else if ((valueA.endsWith("M") && valueB.endsWith("M")) ||
                        valueA.endsWith("G") && valueB.endsWith("G")) {
                        Float widthA = Float.parseFloat(valueA.substring(0, valueA.length() - 1));
                        Float widthB = Float.parseFloat(valueB.substring(0, valueB.length() - 1));
                        return widthA > widthB ? 1 : -1;
                    } else if (valueA.endsWith("M") && valueB.endsWith("G")) {
                        return -1;
                    } else {
                        return 1;
                    }
                }).collect(Collectors.toList());
            AtomicInteger index = new AtomicInteger(0);

            attrValues.stream().filter(s -> {
                index.getAndIncrement();
                // 100M
                return KiteStringUtils.equals(s.getAttrValue(), "1_0000040");
            }).findFirst();
            int indexOf100M = index.get() - 1;

            index.set(0);
            attrValues.stream().filter(s -> {
                index.getAndIncrement();
                // 1G
                return KiteStringUtils.equals(s.getAttrValue(), "1_0000066");
            }).findFirst();
            int indexOf1G = index.get() - 1;

            index.set(0);
            attrValues.stream().filter(s -> {
                index.getAndIncrement();
                // 10G
                return KiteStringUtils.equals(s.getAttrValue(), "1_0000065");
            }).findFirst();
            int indexOf10G = index.get() - 1;
            String baseValue = basedBandwidthAttr.getValueDesc();
            Float baseValueFloat;
            if (KiteStringUtils.endsWith(basedBandwidthAttr.getValueDesc(), "M")) {
                baseValue = baseValue.substring(0, baseValue.length() - 1);
                baseValueFloat = Float.parseFloat(baseValue);
                // 小于100M
                if (baseValueFloat < 100) {
                    resAttr.setAttrValues(attrValues.subList(0, indexOf100M));
                } else {
                    resAttr.setAttrValues(attrValues.subList(indexOf100M, indexOf1G));
                }
            } else if (KiteStringUtils.endsWithAny(basedBandwidthAttr.getValueDesc(), "G")) {
                baseValue = baseValue.substring(0, baseValue.length() - 1);
                baseValueFloat = Float.parseFloat(baseValue);
                // 小于10G
                if (baseValueFloat < 10) {
                    resAttr.setAttrValues(getNeedAttrValuesList(attrValues.subList(indexOf1G, indexOf10G),wuAttrValue));
                } else {
                    resAttr.setAttrValues(getNeedAttrValuesList(attrValues.subList(indexOf10G, attrValues.size()),wuAttrValue));
                }
            }
        }
    }

    public static List<KiteAttrValue> getNeedAttrValuesList(List<KiteAttrValue> kiteAttrValues,AttrValue wuAttrValue){
        //如果没有枚举值：无，则添加一个
        KiteAttrValue wuKiteAttr = kiteAttrValues.stream().filter(targetAttr -> KiteStringUtils.equals(targetAttr.getAttrValue(), "1_0000260")).findFirst().orElse(null);
        if (!Objects.nonNull(wuKiteAttr)) {
            KiteAttrValue  kiteAttrValue = new KiteAttrValue();
            kiteAttrValue.setAttrId(wuAttrValue.getAttrId());
            kiteAttrValue.setAttrValue(wuAttrValue.getAttrValue());
            kiteAttrValue.setAttrValueDesc(wuAttrValue.getAttrValueDesc());
            kiteAttrValue.setAttrValueName(wuAttrValue.getAttrValueName());
            kiteAttrValues.add(kiteAttrValue);
        }
        return kiteAttrValues;
    }
}
